home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / tools / ppm2tiff.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  5KB  |  170 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/ppm2tiff.c,v 1.5 92/02/10 19:04:14 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include "tiffio.h"
  31.  
  32. typedef    unsigned char u_char;
  33. typedef    unsigned short u_short;
  34. typedef    unsigned long u_long;
  35.  
  36. #define    howmany(x, y)    (((x)+((y)-1))/(y))
  37. #define    streq(a,b)    (strcmp(a,b) == 0)
  38.  
  39. u_short    compression = -1;
  40. u_short    photometric;
  41. u_long    rowsperstrip = (u_long) -1;
  42.  
  43. static
  44. BadPPM(file)
  45.     char *file;
  46. {
  47.     fprintf(stderr, "%s: Not a PPM file.\n", file);
  48.     exit(-2);
  49. }
  50.  
  51. main(argc, argv)
  52.     char *argv[];
  53. {
  54.     u_char *buf;
  55.     int row, linebytes, spp;
  56.     TIFF *out;
  57.     FILE *in;
  58.     int c, w, h, prec;
  59.     char *infile;
  60.  
  61.     argc--, argv++;
  62.     if (argc < 1)
  63.         usage();
  64.     for (; argc > 1 && argv[0][0] == '-'; argc--, argv++) {
  65.         if (streq(argv[0], "-none")) {
  66.             compression = COMPRESSION_NONE;
  67.             continue;
  68.         }
  69.         if (streq(argv[0], "-packbits")) {
  70.             compression = COMPRESSION_PACKBITS;
  71.             continue;
  72.         }
  73.         if (streq(argv[0], "-lzw")) {
  74.             compression = COMPRESSION_LZW;
  75.             continue;
  76.         }
  77.         if (streq(argv[0], "-rowsperstrip")) {
  78.             argc--, argv++;
  79.             rowsperstrip = atoi(argv[0]);
  80.             continue;
  81.         }
  82.         usage();
  83.     }
  84.  
  85.     /*
  86.      * If only one file is specified, read input from
  87.      * stdin; otherwise usage is: ppm2tiff input output.
  88.      */
  89.     if (argc > 1) {
  90.         infile = argv[0];
  91.         argv++;
  92.         in = fopen(infile, "r");
  93.         if (in == NULL) {
  94.             fprintf(stderr, "%s: Can not open.\n", infile);
  95.             exit(-1);
  96.         }
  97.     } else {
  98.         infile = "<stdin>";
  99.         in = stdin;
  100.     }
  101.  
  102.     if (getc(in) != 'P')
  103.         BadPPM(infile);
  104.     switch (c = getc(in)) {
  105.     case '5':            /* it's a PGM file */
  106.         spp = 1;
  107.         photometric = PHOTOMETRIC_MINISBLACK;
  108.         break;
  109.     case '6':            /* it's a PPM file */
  110.         spp = 3;
  111.         photometric = PHOTOMETRIC_RGB;
  112.         break;
  113.     default:
  114.         BadPPM(infile);
  115.     }
  116.     if (fscanf(in, " %d %d %d", &w, &h, &prec) != 3)
  117.         BadPPM(infile);
  118.     if (getc(in) != '\n' || w <= 0 || h <= 0 || prec != 255)
  119.         BadPPM(infile);
  120.  
  121.     out = TIFFOpen(argv[0], "w");
  122.     if (out == NULL)
  123.         exit(-4);
  124.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (u_long) w);
  125.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, (u_long) h);
  126.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  127.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
  128.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
  129.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  130.     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
  131.     if (compression == (u_short)-1)
  132.         compression = COMPRESSION_LZW;
  133.     TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  134.     linebytes = spp * w;
  135.     if (TIFFScanlineSize(out) > linebytes)
  136.         buf = (u_char *)malloc(linebytes);
  137.     else
  138.         buf = (u_char *)malloc(TIFFScanlineSize(out));
  139.     if (rowsperstrip == (u_long)-1)
  140.         rowsperstrip = (8*1024)/linebytes;
  141.     TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  142.         rowsperstrip == 0 ? 1L : rowsperstrip);
  143.     for (row = 0; row < h; row++) {
  144.         if (fread(buf, linebytes, 1, in) != 1) {
  145.             fprintf(stderr, "%s: scanline %d: Read error.\n",
  146.                 infile, row);
  147.             break;
  148.         }
  149.         if (TIFFWriteScanline(out, buf, row, 0) < 0)
  150.             break;
  151.     }
  152.     (void) TIFFClose(out);
  153. }
  154.  
  155. usage()
  156. {
  157.     fprintf(stderr, "usage: ppm2tif [options] [input] output\n");
  158.     fprintf(stderr, "where options are:\n");
  159.     fprintf(stderr,
  160.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  161.     fprintf(stderr,
  162.         " -packbits\tcompress output with packbits encoding\n");
  163.     fprintf(stderr,
  164.         " -none\t\tuse no compression algorithm on output\n");
  165.     fprintf(stderr, "\n");
  166.     fprintf(stderr,
  167.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  168.     exit(-1);
  169. }
  170.